package com.register.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.register.dao.UserMapper;
import com.register.pojo.SysUsers;
import com.register.service.UserService;
import com.register.util.RSAUtils;
import com.register.util.Result;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUsers> implements UserService {

    @Resource
    UserMapper userMapper;
    @Value("${jwt.publicKey}")
    String publicKey;
    @Value("${jwt.privateKey}")
    String privateKey;
    //添加用户
    public Result addUser (SysUsers sysUsers) {

        //验证用户名
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", sysUsers.getUsername());
        queryWrapper.eq("phone", sysUsers.getPhone());
        int num = userMapper.selectCount(queryWrapper);
        if (num > 0) {
            //用户名占用
            return Result.build(1,"用户名或者手机号被占用");
        }
        //密码加密
        try{
            byte[] pwd = RSAUtils.encryptByPublicKey(sysUsers.getPassword().getBytes(), Base64.decode(publicKey));
            sysUsers.setPassword(Base64.encode(pwd));
        }catch (Exception e){

            return  Result.build(1,"加密失败");
        }
        userMapper.insert(sysUsers);
        return  Result.ok();
    }

    public Result update (SysUsers sysUsers) {

        //验证用户名与手机号
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", sysUsers.getUsername());
        queryWrapper.eq("phone",sysUsers.getPhone());
        queryWrapper.ne("id",sysUsers.getId());
        int userNum = userMapper.selectCount(queryWrapper);
        if (userNum > 0) {
            //用户名占用
            return Result.build(1,"用户名或者手机号被占用");
        }
        SysUsers sysUsersOld = userMapper.selectById(sysUsers.getId());
        //判断
        if (!sysUsersOld.getPassword().equals(sysUsers.getPassword())){

            //密码修改了  将新密码加密
            try{
                byte[] pwd = RSAUtils.encryptByPublicKey(sysUsers.getPassword().getBytes(), Base64.decode(publicKey));
                sysUsers.setPassword(Base64.encode(pwd));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //更新
        int updataNum = userMapper.updateById(sysUsers);
        if(updataNum == 1){

            return Result.ok();
        }else {
            return Result.build(1,"更新失败");
        }
    }
}
